package top.soulblack.purchasesale.service;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.soulblack.purchasesale.common.enums.OrderStatusEnum;
import top.soulblack.purchasesale.common.enums.OrderTypeEnum;
import top.soulblack.purchasesale.common.enums.ResultEnum;
import top.soulblack.purchasesale.exception.PurchaseException;
import top.soulblack.purchasesale.mapper.InOutOrderMapper;
import top.soulblack.purchasesale.model.DTO.OrderDTO;
import top.soulblack.purchasesale.model.InOutOrder;
import top.soulblack.purchasesale.model.OrderDetail;
import top.soulblack.purchasesale.model.OrderMaster;
import top.soulblack.purchasesale.model.StoreHouse;
import top.soulblack.purchasesale.model.VO.InOrOutOrderVO;
import top.soulblack.purchasesale.model.VO.InOrderVO;
import top.soulblack.purchasesale.model.VO.OrderDetailVO;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Author: 韦溜溜
 * @Description:
 * @Date: Create in 21:44 2019/8/25
 */
@Service
@Slf4j
public class InOutOrderService {

    @Autowired
    private InOutOrderMapper inOutOrderMapper;

    @Autowired
    private StoreHouseService storeHouseService;

    @Autowired
    private SupplierService supplierService;

    @Autowired
    private OrderDetailService orderDetailService;



    public Boolean createOutOrder(String orderId){
        InOutOrder inOutOrder = new InOutOrder();
//        inOutOrder.setOrderId(orderId);
        inOutOrder.setStatus(OrderStatusEnum.WAITINOROUT.getCode());
        inOutOrder.setType(OrderTypeEnum.OUTORDER.getCode());
        int num = inOutOrderMapper.insert(inOutOrder);
        if(num>=1){
            return true;
        }
        else{
            log.error("【创建出库单】插入失败 orderId={}",orderId);
            throw new PurchaseException(ResultEnum.INSERTOUTORDERERROR);
        }
    }
    public List<InOrOutOrderVO> selectInOrder(){
        List<InOrOutOrderVO> inOrOutOrderVOS;
        inOrOutOrderVOS = inOutOrderMapper.selectInOrder();
        return inOrOutOrderVOS;
    }

    public InOrderVO getOneInOrderVO(String orderId){
        InOrderVO inOrderVO = new InOrderVO();
        InOutOrder inOutOrder = inOutOrderMapper.selectInOrderById(orderId);
        if(inOutOrder.getSupOrCusId()!= null){
            BeanUtils.copyProperties(supplierService.selectById(inOutOrder.getSupOrCusId()),inOrderVO);
        }
        inOrderVO.setRemarks(inOutOrder.getRemarks());
        inOrderVO.setUpdateTime(inOutOrder.getUpdateTime());
        Map<Integer,StoreHouse> storeHouseMap = storeHouseService.getStoreToMap();
        inOrderVO.setStoreName(storeHouseMap.get(inOutOrder.getInStoreId()).getStoreName());

        inOrderVO.setOrderDetailVOS(orderDetailService.getVOByOrderId(orderId));

        return inOrderVO;
    }

}
